﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PdfLibrary
{
    public class PdfIndirectReference : IPdfObject
    {
        public int objectNumber;
        public int generationNumber;
        public IPdfIndirectReferenceResolver referenceResolver;

        public bool LoadFromReader(PdfReader reader, bool throwException)
        {
            if (!int.TryParse(reader.ReadWord(), out this.objectNumber))
                if (throwException) throw new PdfParsingException(reader, "PdfIndirectReference parsing error: objectNumber is not int!");
                else return false;

            if (!int.TryParse(reader.ReadWord(), out this.generationNumber))
                if (throwException) throw new PdfParsingException(reader, "PdfIndirectReference parsing error: generationNumber is not int!");
                else return false;

            if (reader.ReadWord() != "R")
                if (throwException) throw new PdfParsingException(reader, "PdfIndirectReference parsing error: 'R' not found!");
                else return false;

            return true;
        }

        /*public T Resolve<T>() where T: PdfObject
        {
            return (T)ResolvedObject;
        }*/

        public IPdfObject ResolvedObject
        {
            get { return referenceResolver.Resolve(this); }
        }
    }
}
